using System.Windows;
using System.Windows.Controls;
using MiniTheme.ViewModels;

namespace MiniTheme.Views;

public partial class MainWindow : Window
{
    public MainWindow(MainViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;

        // Hook up window control buttons
        this.Loaded += (s, e) =>
        {
            var minimizeButton = (Button)this.Template.FindName("MinimizeButton", this);
            var maximizeRestoreButton = (Button)this.Template.FindName("MaximizeRestoreButton", this);
            var closeButton = (Button)this.Template.FindName("CloseButton", this);

            if (minimizeButton != null)
                minimizeButton.Click += (_, _) => this.WindowState = WindowState.Minimized;

            if (maximizeRestoreButton != null)
            {
                maximizeRestoreButton.Click += (_, _) =>
                {
                    this.WindowState = this.WindowState == WindowState.Maximized
                        ? WindowState.Normal
                        : WindowState.Maximized;
                };

                // Update button icon when window state changes
                this.StateChanged += (_, _) =>
                {
                    maximizeRestoreButton.Content = this.WindowState == WindowState.Maximized
                        ? "\uE923"  // Restore icon
                        : "\uE922"; // Maximize icon
                };
            }

            if (closeButton != null)
                closeButton.Click += (_, _) => this.Close();
        };
    }
}
